/*
 * @Author: Haochen
 * @Date: 2021-11-22 15:01:04
 * @LastEditTime: 2021-11-26 14:57:47
 * @FilePath: \front\src\js\exam_score.js
 */

import $ from './jquery-3.6.0.min.js';
import '../scss/common.scss';
import '../scss/exam-score.scss';

import { backToIndex, getPromise } from './common.js';

backToIndex();

const testType = window.location.search
  .substring(1)
  .split('?')[0]
  .split('=')
  .pop();
const testId = window.location.search.substring(1).split('=').pop();

const users = JSON.parse(localStorage.getItem('users'));
const userId = users.find((user) => user.status === 'online')._id;

$(function () {
  async function getDataAndRender() {
    let { code, data, message } = await getPromise(
      '/testedsRouter/:' + userId,
      'get',
      {}
    );

    data = data.find((test) => test.testId._id == testId);

    $('.p-name').text(data.studentId.name);
    $('#score').text(data.score);
    $('.full-marks').text('15');
    $('.exam-duration').text(data.timeUsed);
  }

  getDataAndRender();
});

$('#btn-back-exam').click(function () {
  let timer = setTimeout(function () {
    location.href = `./exam.html?type=${testType}`;
  }, 500);
});

$('#btn-exam-detail').click(function () {
  location.href = `./exam_detail.html?id=${testId}`;
});
